package com.jmpt.fangbohui.push;

import java.util.Set;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.MyApplication;
import com.jmpt.fangbohui.utils.DebugLog;


public class JPushUtils {
    public static final String TAG = JPushUtils.class.getSimpleName();
    Context context;

    public JPushUtils(Context context) {
        this.context = context;
    }

    public void setAlias(int userid) {
        String aliasName = String.valueOf(userid);
        String recordName = getSetAliasName();
        if (aliasName.equals(recordName) == false) {
            setAlias(aliasName);
        }

    }

    public void setAlias(String name) {
        JPushInterface.setAlias(context, name, mAliasCallback);
    }

    public String getSetAliasName() {
        return MyApplication.getSharedPreferences().getString(Constants.SharedPreferences.JPUSH_USER_ID, "");
    }

    public void setAliasName(String aliasName) {
        MyApplication.getSharedPreferences().edit().putString(Constants.SharedPreferences.JPUSH_USER_ID, aliasName).commit();
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i(TAG, logs);
                    DebugLog.i("alias:" + alias);
                    setAliasName(alias);
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i(TAG, logs);
                    // 延迟 60 秒来调用 Handler 设置别名
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.e(TAG, logs);
            }


        }
    };

    private static final int MSG_SET_ALIAS = 1001;
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    Log.d(TAG, "Set alias in handler.");
                    // 调用 JPush 接口来设置别名。
                    JPushInterface.setAliasAndTags(context.getApplicationContext(), (String) msg.obj, null, mAliasCallback);
                    break;
                default:
                    Log.i(TAG, "Unhandled msg - " + msg.what);
            }
        }
    };
}
